# installing packages
!pip install --upgrade plotly # if this does not work, !pip install --upgrade plotly -- user
import plotly
print(plotly.__version__)
4.14.3
ERROR: Invalid requirement: '#'
# Checking installed plotly version
import pandas as pd
import plotly
import plotly.graph_objects as go
import plotly.express as px
pd.set_option('display.max_rows', 500)
print(plotly.__version__)
import warnings
warnings.filterwarnings('ignore')
4.14.3
# video game data
df = pd.read_csv('https://raw.githubusercontent.com/bonchae/data/master/vgsales.csv')
df.dropna(inplace=True) # drop rows with missing values
df
| Rank | Name | Platform | Year | Genre | Publisher | NA_Sales | EU_Sales | JP_Sales | Other_Sales | Global_Sales | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | Wii Sports | Wii | 2006.0 | Sports | Nintendo | 41.49 | 29.02 | 3.77 | 8.46 | 82.74 |
| 1 | 2 | Super Mario Bros. | NES | 1985.0 | Platform | Nintendo | 29.08 | 3.58 | 6.81 | 0.77 | 40.24 |
| 2 | 3 | Mario Kart Wii | Wii | 2008.0 | Racing | Nintendo | 15.85 | 12.88 | 3.79 | 3.31 | 35.82 |
| 3 | 4 | Wii Sports Resort | Wii | 2009.0 | Sports | Nintendo | 15.75 | 11.01 | 3.28 | 2.96 | 33.00 |
| 4 | 5 | Pokemon Red/Pokemon Blue | GB | 1996.0 | Role-Playing | Nintendo | 11.27 | 8.89 | 10.22 | 1.00 | 31.37 |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 16593 | 16596 | Woody Woodpecker in Crazy Castle 5 | GBA | 2002.0 | Platform | Kemco | 0.01 | 0.00 | 0.00 | 0.00 | 0.01 |
| 16594 | 16597 | Men in Black II: Alien Escape | GC | 2003.0 | Shooter | Infogrames | 0.01 | 0.00 | 0.00 | 0.00 | 0.01 |
| 16595 | 16598 | SCORE International Baja 1000: The Official Game | PS2 | 2008.0 | Racing | Activision | 0.00 | 0.00 | 0.00 | 0.00 | 0.01 |
| 16596 | 16599 | Know How 2 | DS | 2010.0 | Puzzle | 7G//AMES | 0.00 | 0.01 | 0.00 | 0.00 | 0.01 |
| 16597 | 16600 | Spirits & Spells | GBA | 2003.0 | Platform | Wanadoo | 0.01 | 0.00 | 0.00 | 0.00 | 0.01 |
16291 rows × 11 columns
This dataset contains a list of video games with sales greater than 100,000 copies. It was generated by a scrape of vgchartz.com.
Fields include
The script to scrape the data is available at https://github.com/GregorUT/vgchartzScrape. It is based on BeautifulSoup using Python. There are 16,598 records. 2 records were dropped due to incomplete information.
# I am interested in two columns: Platform, Genre
# What Platform is most popular?
df.groupby('Platform').size().sort_values(ascending=False).head()
Platform DS 2131 PS2 2127 PS3 1304 Wii 1290 X360 1234 dtype: int64
px.treemap(df, path=['Platform'])
# Which genre is most popolar?
df.groupby('Genre').size().sort_values(ascending=False).head()
Genre Action 3251 Sports 2304 Misc 1686 Role-Playing 1470 Shooter 1282 dtype: int64
px.treemap(df, path=['Genre'])
# pivot table for two columns
df.groupby(['Platform','Genre']).size()
Platform Genre
2600 Action 55
Adventure 1
Fighting 1
Misc 5
Platform 9
Puzzle 8
Racing 4
Shooter 22
Simulation 1
Sports 10
3DO Adventure 1
Puzzle 1
Simulation 1
3DS Action 180
Adventure 36
Fighting 14
Misc 53
Platform 28
Puzzle 19
Racing 10
Role-Playing 85
Shooter 6
Simulation 28
Sports 25
Strategy 15
DC Action 3
Adventure 11
Fighting 12
Platform 2
Racing 6
Role-Playing 4
Shooter 3
Simulation 1
Sports 10
DS Action 335
Adventure 238
Fighting 36
Misc 389
Platform 89
Puzzle 236
Racing 66
Role-Playing 196
Shooter 42
Simulation 280
Sports 146
Strategy 78
GB Action 6
Adventure 5
Misc 8
Platform 18
Puzzle 15
Racing 2
Role-Playing 21
Shooter 1
Simulation 5
Sports 9
Strategy 7
GBA Action 162
Adventure 36
Fighting 23
Misc 86
Platform 139
Puzzle 39
Racing 64
Role-Playing 73
Shooter 40
Simulation 18
Sports 88
Strategy 18
GC Action 98
Adventure 20
Fighting 42
Misc 35
Platform 73
Puzzle 12
Racing 60
Role-Playing 25
Shooter 48
Simulation 12
Sports 106
Strategy 11
GEN Action 3
Adventure 2
Fighting 5
Misc 1
Platform 7
Racing 1
Role-Playing 3
Shooter 1
Sports 3
Strategy 1
GG Platform 1
N64 Action 37
Adventure 4
Fighting 29
Misc 18
Platform 30
Puzzle 12
Racing 57
Role-Playing 7
Shooter 24
Simulation 10
Sports 79
Strategy 9
NES Action 13
Adventure 1
Fighting 4
Misc 2
Platform 28
Puzzle 14
Racing 4
Role-Playing 11
Shooter 7
Sports 14
NG Fighting 11
Sports 1
PC Action 161
Adventure 65
Fighting 5
Misc 22
Platform 11
Puzzle 25
Racing 56
Role-Playing 103
Shooter 145
Simulation 112
Sports 49
Strategy 184
PCFX Role-Playing 1
PS Action 154
Adventure 67
Fighting 108
Misc 76
Platform 64
Puzzle 32
Racing 144
Role-Playing 97
Shooter 96
Simulation 60
Sports 221
Strategy 70
PS2 Action 345
Adventure 196
Fighting 146
Misc 218
Platform 101
Puzzle 18
Racing 212
Role-Playing 183
Shooter 158
Simulation 89
Sports 391
Strategy 70
PS3 Action 373
Adventure 72
Fighting 76
Misc 121
Platform 36
Puzzle 2
Racing 90
Role-Playing 117
Shooter 155
Simulation 30
Sports 210
Strategy 22
PS4 Action 122
Adventure 19
Fighting 17
Misc 15
Platform 11
Puzzle 1
Racing 17
Role-Playing 47
Shooter 34
Simulation 5
Sports 43
Strategy 5
PSP Action 217
Adventure 213
Fighting 73
Misc 104
Platform 36
Puzzle 44
Racing 65
Role-Playing 191
Shooter 37
Simulation 29
Sports 130
Strategy 58
PSV Action 141
Adventure 85
Fighting 16
Misc 24
Platform 10
Puzzle 3
Racing 11
Role-Playing 82
Shooter 5
Simulation 3
Sports 23
Strategy 7
SAT Action 3
Adventure 26
Fighting 31
Misc 15
Platform 5
Puzzle 5
Racing 8
Role-Playing 17
Shooter 22
Simulation 7
Sports 16
Strategy 18
SCD Misc 2
Platform 1
Racing 1
Role-Playing 1
Strategy 1
SNES Action 12
Adventure 4
Fighting 25
Misc 17
Platform 26
Puzzle 13
Racing 9
Role-Playing 50
Shooter 10
Simulation 9
Sports 49
Strategy 15
TG16 Adventure 1
Shooter 1
WS Role-Playing 4
Strategy 2
Wii Action 230
Adventure 83
Fighting 41
Misc 272
Platform 57
Puzzle 54
Racing 92
Role-Playing 35
Shooter 61
Simulation 84
Sports 256
Strategy 25
WiiU Action 63
Adventure 3
Fighting 5
Misc 21
Platform 16
Puzzle 4
Racing 3
Role-Playing 6
Shooter 10
Simulation 1
Sports 8
Strategy 3
X360 Action 318
Adventure 47
Fighting 65
Misc 122
Platform 24
Puzzle 6
Racing 102
Role-Playing 75
Shooter 197
Simulation 36
Sports 215
Strategy 27
XB Action 152
Adventure 26
Fighting 44
Misc 45
Platform 49
Puzzle 7
Racing 122
Role-Playing 23
Shooter 124
Simulation 24
Sports 166
Strategy 21
XOne Action 68
Adventure 12
Fighting 7
Misc 15
Platform 4
Racing 19
Role-Playing 13
Shooter 33
Simulation 3
Sports 36
Strategy 3
dtype: int64
px.treemap(df, path=['Platform', 'Genre'])
# pivot ... find Global Sales per Platform and Genre
df.groupby(['Platform','Genre'])['Global_Sales'].sum()
Platform Genre
2600 Action 26.39
Adventure 0.40
Fighting 0.77
Misc 3.58
Platform 13.27
Puzzle 13.65
Racing 2.32
Shooter 22.97
Simulation 0.45
Sports 2.77
3DO Adventure 0.06
Puzzle 0.02
Simulation 0.02
3DS Action 56.61
Adventure 4.73
Fighting 10.46
Misc 10.48
Platform 32.23
Puzzle 5.56
Racing 14.41
Role-Playing 75.71
Shooter 1.22
Simulation 26.59
Sports 6.18
Strategy 2.09
DC Action 1.26
Adventure 2.50
Fighting 1.83
Platform 2.54
Racing 2.65
Role-Playing 0.68
Shooter 0.33
Simulation 0.52
Sports 3.66
DS Action 114.16
Adventure 47.15
Fighting 7.20
Misc 137.67
Platform 77.40
Puzzle 83.87
Racing 38.58
Role-Playing 126.56
Shooter 8.20
Simulation 131.65
Sports 31.71
Strategy 14.76
GB Action 7.92
Adventure 17.16
Misc 13.35
Platform 53.88
Puzzle 47.47
Racing 4.55
Role-Playing 88.24
Shooter 1.20
Simulation 3.55
Sports 9.05
Strategy 8.05
GBA Action 54.26
Adventure 12.10
Fighting 4.21
Misc 28.50
Platform 78.08
Puzzle 12.09
Racing 18.80
Role-Playing 64.21
Shooter 3.60
Simulation 5.91
Sports 16.41
Strategy 7.45
GC Action 37.25
Adventure 5.93
Fighting 18.43
Misc 16.45
Platform 28.66
Puzzle 4.57
Racing 21.79
Role-Playing 13.03
Shooter 13.63
Simulation 8.59
Sports 24.49
Strategy 4.32
GEN Action 2.74
Adventure 0.19
Fighting 5.90
Misc 0.03
Platform 15.45
Racing 0.26
Role-Playing 0.27
Shooter 0.13
Sports 3.20
Strategy 0.19
GG Platform 0.04
N64 Action 29.44
Adventure 0.45
Fighting 22.08
Misc 11.19
Platform 36.13
Puzzle 3.41
Racing 40.09
Role-Playing 3.03
Shooter 19.27
Simulation 10.19
Sports 32.60
Strategy 10.33
NES Action 28.75
Adventure 4.38
Fighting 6.54
Misc 3.59
Platform 95.78
Puzzle 21.00
Racing 9.78
Role-Playing 18.78
Shooter 35.62
Sports 26.85
NG Fighting 1.42
Sports 0.02
PC Action 30.67
Adventure 10.09
Fighting 0.14
Misc 8.41
Platform 0.49
Puzzle 0.92
Racing 3.80
Role-Playing 47.37
Shooter 43.44
Simulation 51.73
Sports 12.01
Strategy 45.63
PCFX Role-Playing 0.03
PS Action 125.74
Adventure 20.77
Fighting 72.68
Misc 44.90
Platform 64.21
Puzzle 12.08
Racing 102.89
Role-Playing 78.30
Shooter 39.31
Simulation 25.33
Sports 119.51
Strategy 21.67
PS2 Action 272.43
Adventure 21.16
Fighting 89.19
Misc 98.69
Platform 72.11
Puzzle 5.90
Racing 154.21
Role-Playing 91.55
Shooter 108.28
Simulation 42.26
Sports 262.64
Strategy 15.04
PS3 Action 304.02
Adventure 22.87
Fighting 51.70
Misc 45.91
Platform 29.85
Puzzle 0.46
Racing 73.10
Role-Playing 75.24
Shooter 195.80
Simulation 10.72
Sports 134.91
Strategy 4.77
PS4 Action 87.06
Adventure 4.70
Fighting 8.04
Misc 7.40
Platform 7.01
Puzzle 0.02
Racing 11.53
Role-Playing 25.77
Shooter 75.32
Simulation 0.77
Sports 50.07
Strategy 0.41
PSP Action 62.66
Adventure 10.69
Fighting 21.88
Misc 13.70
Platform 17.28
Puzzle 5.52
Racing 34.73
Role-Playing 49.01
Shooter 19.77
Simulation 6.28
Sports 39.90
Strategy 10.29
PSV Action 20.00
Adventure 4.16
Fighting 3.15
Misc 5.18
Platform 3.03
Puzzle 0.20
Racing 2.95
Role-Playing 13.09
Shooter 4.57
Simulation 0.08
Sports 4.88
Strategy 0.31
SAT Action 0.65
Adventure 4.16
Fighting 8.52
Misc 1.20
Platform 0.76
Puzzle 1.00
Racing 2.40
Role-Playing 3.76
Shooter 3.98
Simulation 1.13
Sports 2.79
Strategy 3.24
SCD Misc 0.10
Platform 1.50
Racing 0.07
Role-Playing 0.06
Strategy 0.14
SNES Action 10.08
Adventure 1.50
Fighting 26.95
Misc 5.02
Platform 65.65
Puzzle 6.38
Racing 13.49
Role-Playing 36.43
Shooter 6.07
Simulation 5.63
Sports 17.87
Strategy 4.98
TG16 Adventure 0.14
Shooter 0.02
WS Role-Playing 1.22
Strategy 0.20
Wii Action 110.48
Adventure 18.33
Fighting 23.82
Misc 217.43
Platform 90.68
Puzzle 15.63
Racing 61.24
Role-Playing 14.06
Shooter 26.34
Simulation 36.62
Sports 289.95
Strategy 5.23
WiiU Action 19.35
Adventure 0.17
Fighting 6.36
Misc 12.23
Platform 21.24
Puzzle 1.33
Racing 7.77
Role-Playing 2.47
Shooter 6.17
Simulation 0.21
Sports 3.32
Strategy 1.24
X360 Action 239.67
Adventure 15.23
Fighting 37.64
Misc 89.33
Platform 11.39
Puzzle 0.71
Racing 65.13
Role-Playing 71.97
Shooter 277.23
Simulation 14.10
Sports 137.43
Strategy 9.77
XB Action 47.46
Adventure 3.06
Fighting 12.83
Misc 8.67
Platform 9.66
Puzzle 0.42
Racing 31.42
Role-Playing 13.51
Shooter 62.12
Simulation 7.11
Sports 53.05
Strategy 2.78
XOne Action 33.79
Adventure 2.51
Fighting 2.31
Misc 6.86
Platform 0.81
Racing 8.80
Role-Playing 9.48
Shooter 51.61
Simulation 0.54
Sports 23.97
Strategy 0.38
Name: Global_Sales, dtype: float64
px.treemap(df, path=['Platform', 'Genre'], values='Global_Sales')
px.treemap(df, path=['Platform', 'Genre'], values='Global_Sales', color='NA_Sales')
wind = px.data.wind()
wind
| direction | strength | frequency | |
|---|---|---|---|
| 0 | N | 0-1 | 0.50 |
| 1 | NNE | 0-1 | 0.60 |
| 2 | NE | 0-1 | 0.50 |
| 3 | ENE | 0-1 | 0.40 |
| 4 | E | 0-1 | 0.40 |
| 5 | ESE | 0-1 | 0.30 |
| 6 | SE | 0-1 | 0.40 |
| 7 | SSE | 0-1 | 0.40 |
| 8 | S | 0-1 | 0.60 |
| 9 | SSW | 0-1 | 0.40 |
| 10 | SW | 0-1 | 0.50 |
| 11 | WSW | 0-1 | 0.60 |
| 12 | W | 0-1 | 0.60 |
| 13 | WNW | 0-1 | 0.50 |
| 14 | NW | 0-1 | 0.40 |
| 15 | NNW | 0-1 | 0.10 |
| 16 | N | 1-2 | 1.60 |
| 17 | NNE | 1-2 | 1.80 |
| 18 | NE | 1-2 | 1.50 |
| 19 | ENE | 1-2 | 1.60 |
| 20 | E | 1-2 | 1.60 |
| 21 | ESE | 1-2 | 1.20 |
| 22 | SE | 1-2 | 1.50 |
| 23 | SSE | 1-2 | 1.70 |
| 24 | S | 1-2 | 2.20 |
| 25 | SSW | 1-2 | 2.00 |
| 26 | SW | 1-2 | 2.30 |
| 27 | WSW | 1-2 | 2.40 |
| 28 | W | 1-2 | 2.30 |
| 29 | WNW | 1-2 | 2.60 |
| 30 | NW | 1-2 | 2.30 |
| 31 | NNW | 1-2 | 0.80 |
| 32 | N | 2-3 | 0.90 |
| 33 | NNE | 2-3 | 1.30 |
| 34 | NE | 2-3 | 1.60 |
| 35 | ENE | 2-3 | 0.90 |
| 36 | E | 2-3 | 1.00 |
| 37 | ESE | 2-3 | 0.60 |
| 38 | SE | 2-3 | 0.60 |
| 39 | SSE | 2-3 | 0.90 |
| 40 | S | 2-3 | 1.40 |
| 41 | SSW | 2-3 | 1.70 |
| 42 | SW | 2-3 | 1.90 |
| 43 | WSW | 2-3 | 2.20 |
| 44 | W | 2-3 | 1.80 |
| 45 | WNW | 2-3 | 1.70 |
| 46 | NW | 2-3 | 1.80 |
| 47 | NNW | 2-3 | 0.80 |
| 48 | N | 3-4 | 0.90 |
| 49 | NNE | 3-4 | 0.80 |
| 50 | NE | 3-4 | 1.20 |
| 51 | ENE | 3-4 | 1.00 |
| 52 | E | 3-4 | 0.80 |
| 53 | ESE | 3-4 | 0.40 |
| 54 | SE | 3-4 | 0.50 |
| 55 | SSE | 3-4 | 0.50 |
| 56 | S | 3-4 | 0.80 |
| 57 | SSW | 3-4 | 0.90 |
| 58 | SW | 3-4 | 1.30 |
| 59 | WSW | 3-4 | 1.10 |
| 60 | W | 3-4 | 1.20 |
| 61 | WNW | 3-4 | 1.20 |
| 62 | NW | 3-4 | 1.30 |
| 63 | NNW | 3-4 | 1.00 |
| 64 | N | 4-4 | 0.40 |
| 65 | NNE | 4-4 | 0.50 |
| 66 | NE | 4-4 | 1.20 |
| 67 | ENE | 4-4 | 0.50 |
| 68 | E | 4-4 | 0.40 |
| 69 | ESE | 4-4 | 0.20 |
| 70 | SE | 4-4 | 0.40 |
| 71 | SSE | 4-4 | 0.40 |
| 72 | S | 4-4 | 0.70 |
| 73 | SSW | 4-4 | 0.60 |
| 74 | SW | 4-4 | 0.70 |
| 75 | WSW | 4-4 | 0.80 |
| 76 | W | 4-4 | 0.90 |
| 77 | WNW | 4-4 | 1.00 |
| 78 | NW | 4-4 | 1.00 |
| 79 | NNW | 4-4 | 0.70 |
| 80 | N | 4-5 | 0.30 |
| 81 | NNE | 4-5 | 0.30 |
| 82 | NE | 4-5 | 0.60 |
| 83 | ENE | 4-5 | 0.20 |
| 84 | E | 4-5 | 0.10 |
| 85 | ESE | 4-5 | 0.10 |
| 86 | SE | 4-5 | 0.05 |
| 87 | SSE | 4-5 | 0.10 |
| 88 | S | 4-5 | 0.10 |
| 89 | SSW | 4-5 | 0.20 |
| 90 | SW | 4-5 | 0.30 |
| 91 | WSW | 4-5 | 0.40 |
| 92 | W | 4-5 | 0.90 |
| 93 | WNW | 4-5 | 0.90 |
| 94 | NW | 4-5 | 0.90 |
| 95 | NNW | 4-5 | 0.30 |
| 96 | N | 5-6 | 0.20 |
| 97 | NNE | 5-6 | 0.10 |
| 98 | NE | 5-6 | 0.10 |
| 99 | ENE | 5-6 | 0.10 |
| 100 | E | 5-6 | 0.10 |
| 101 | ESE | 5-6 | 0.10 |
| 102 | SE | 5-6 | 0.05 |
| 103 | SSE | 5-6 | 0.05 |
| 104 | S | 5-6 | 0.10 |
| 105 | SSW | 5-6 | 0.05 |
| 106 | SW | 5-6 | 0.20 |
| 107 | WSW | 5-6 | 0.20 |
| 108 | W | 5-6 | 0.40 |
| 109 | WNW | 5-6 | 0.70 |
| 110 | NW | 5-6 | 0.70 |
| 111 | NNW | 5-6 | 0.40 |
| 112 | N | 6+ | 0.10 |
| 113 | NNE | 6+ | 0.10 |
| 114 | NE | 6+ | 0.10 |
| 115 | ENE | 6+ | 0.10 |
| 116 | E | 6+ | 0.10 |
| 117 | ESE | 6+ | 0.05 |
| 118 | SE | 6+ | 0.05 |
| 119 | SSE | 6+ | 0.05 |
| 120 | S | 6+ | 0.05 |
| 121 | SSW | 6+ | 0.10 |
| 122 | SW | 6+ | 0.10 |
| 123 | WSW | 6+ | 0.10 |
| 124 | W | 6+ | 0.90 |
| 125 | WNW | 6+ | 2.20 |
| 126 | NW | 6+ | 1.50 |
| 127 | NNW | 6+ | 0.20 |
# Wind Rose Chart with plotly express https://plot.ly/python/wind-rose-charts/
px.bar_polar(wind, r="frequency", theta="direction",
color="strength", template="plotly_dark",
color_discrete_sequence= px.colors.sequential.Plasma[-2::-1])
ap = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv")
ap.head()
| Date | AAPL.Open | AAPL.High | AAPL.Low | AAPL.Close | AAPL.Volume | AAPL.Adjusted | dn | mavg | up | direction | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 2015-02-17 | 127.489998 | 128.880005 | 126.919998 | 127.830002 | 63152400 | 122.905254 | 106.741052 | 117.927667 | 129.114281 | Increasing |
| 1 | 2015-02-18 | 127.629997 | 128.779999 | 127.449997 | 128.720001 | 44891700 | 123.760965 | 107.842423 | 118.940333 | 130.038244 | Increasing |
| 2 | 2015-02-19 | 128.479996 | 129.029999 | 128.330002 | 128.449997 | 37362400 | 123.501363 | 108.894245 | 119.889167 | 130.884089 | Decreasing |
| 3 | 2015-02-20 | 128.619995 | 129.500000 | 128.050003 | 129.500000 | 48948400 | 124.510914 | 109.785449 | 120.763500 | 131.741551 | Increasing |
| 4 | 2015-02-23 | 130.020004 | 133.000000 | 129.660004 | 133.000000 | 70974100 | 127.876074 | 110.372516 | 121.720167 | 133.067817 | Increasing |
# Time Series With Rangeslider https://plot.ly/python/time-series/
fig = go.Figure()
fig.add_trace(go.Scatter(x=ap.Date, y=ap['AAPL.High'], name="AAPL High",
line_color='deepskyblue'))
fig.add_trace(go.Scatter(x=ap.Date, y=ap['AAPL.Low'], name="AAPL Low",
line_color='dimgray'))
fig.update_layout(title_text='Time Series with Rangeslider',
xaxis_rangeslider_visible=True)
fig.show()
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
ue = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
dtype={"fips": str})
ue
| fips | unemp | |
|---|---|---|
| 0 | 01001 | 5.3 |
| 1 | 01003 | 5.4 |
| 2 | 01005 | 8.6 |
| 3 | 01007 | 6.6 |
| 4 | 01009 | 5.5 |
| ... | ... | ... |
| 3214 | 72145 | 13.9 |
| 3215 | 72147 | 10.6 |
| 3216 | 72149 | 20.2 |
| 3217 | 72151 | 16.9 |
| 3218 | 72153 | 18.8 |
3219 rows × 2 columns
# Map https://plot.ly/python/mapbox-county-choropleth/
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=ue.fips, z=ue.unemp,
colorscale="Viridis", zmin=0, zmax=12,
marker_opacity=0.5, marker_line_width=0))
fig.update_layout(mapbox_style="carto-positron",
mapbox_zoom=3, mapbox_center = {"lat": 37.0902, "lon": -95.7129})
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()
# tips: FIPS county code (Riley county 20161) https://www.nrcs.usda.gov/wps/portal/nrcs/detail/ks/home/?cid=nrcs143_013697
ue.loc[ue['fips'] == '20161']
| fips | unemp | |
|---|---|---|
| 967 | 20161 | 3.3 |
pop = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_us_cities.csv')
pop.head()
| name | pop | lat | lon | |
|---|---|---|---|---|
| 0 | New York | 8287238 | 40.730599 | -73.986581 |
| 1 | Los Angeles | 3826423 | 34.053717 | -118.242727 |
| 2 | Chicago | 2705627 | 41.875555 | -87.624421 |
| 3 | Houston | 2129784 | 29.758938 | -95.367697 |
| 4 | Philadelphia | 1539313 | 39.952335 | -75.163789 |
# Bubble map https://plot.ly/python/bubble-maps/
pop['text'] = pop['name'] + '<br>Population ' + (pop['pop']/1e6).astype(str)+' million'
limits = [(0,2),(3,10),(11,20),(21,50),(50,3000)]
colors = ["royalblue","crimson","lightseagreen","orange","lightgrey"]
cities = []
scale = 5000
fig = go.Figure()
for i in range(len(limits)):
lim = limits[i]
df_sub = pop[lim[0]:lim[1]]
fig.add_trace(go.Scattergeo(
locationmode = 'USA-states',
lon = df_sub['lon'],
lat = df_sub['lat'],
text = df_sub['text'],
marker = dict(
size = df_sub['pop']/scale,
color = colors[i],
line_color='rgb(40,40,40)',
line_width=0.5,
sizemode = 'area'
),
name = '{0} - {1}'.format(lim[0],lim[1])))
fig.update_layout(
title_text = '2014 US city populations<br>(Click legend to toggle traces)',
showlegend = True,
geo = dict(
scope = 'usa',
landcolor = 'rgb(217, 217, 217)',
)
)
fig.show()
gapminder = px.data.gapminder()
gapminder.head()
| country | continent | year | lifeExp | pop | gdpPercap | iso_alpha | iso_num | |
|---|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | Asia | 1952 | 28.801 | 8425333 | 779.445314 | AFG | 4 |
| 1 | Afghanistan | Asia | 1957 | 30.332 | 9240934 | 820.853030 | AFG | 4 |
| 2 | Afghanistan | Asia | 1962 | 31.997 | 10267083 | 853.100710 | AFG | 4 |
| 3 | Afghanistan | Asia | 1967 | 34.020 | 11537966 | 836.197138 | AFG | 4 |
| 4 | Afghanistan | Asia | 1972 | 36.088 | 13079460 | 739.981106 | AFG | 4 |
# Animated figures with plotly express (https://plot.ly/python/animations/)
px.scatter(gapminder, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
size="pop", color="continent", hover_name="country", #facet_col="continent"
log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])
# data from Johns Hopkins University
df_map = pd.read_csv('https://raw.githubusercontent.com/bonchae/data/master/df_map.csv')
df_map
| country | date | confirmed | deaths | recovered | Lat | Long | |
|---|---|---|---|---|---|---|---|
| 0 | Afghanistan | 1/22/20 | 0 | 0 | 0 | 33.939110 | 67.709953 |
| 1 | Afghanistan | 1/23/20 | 0 | 0 | 0 | 33.939110 | 67.709953 |
| 2 | Afghanistan | 1/24/20 | 0 | 0 | 0 | 33.939110 | 67.709953 |
| 3 | Afghanistan | 1/25/20 | 0 | 0 | 0 | 33.939110 | 67.709953 |
| 4 | Afghanistan | 1/26/20 | 0 | 0 | 0 | 33.939110 | 67.709953 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 38723 | Zimbabwe | 8/10/20 | 4748 | 104 | 1524 | -19.015438 | 29.154857 |
| 38724 | Zimbabwe | 8/11/20 | 4818 | 104 | 1524 | -19.015438 | 29.154857 |
| 38725 | Zimbabwe | 8/12/20 | 4893 | 122 | 1620 | -19.015438 | 29.154857 |
| 38726 | Zimbabwe | 8/13/20 | 4990 | 128 | 1927 | -19.015438 | 29.154857 |
| 38727 | Zimbabwe | 8/14/20 | 5072 | 128 | 1998 | -19.015438 | 29.154857 |
38728 rows × 7 columns
#call scatter_mapbox function from px. Note the attributes especially normalisation of data and maximum maker size. The animation is done on Dates.
fig_map = px.scatter_mapbox(df_map, lat="Lat", lon="Long", color="confirmed", size=df_map['confirmed']**0.5*50,
color_continuous_scale="Rainbow", size_max=50, animation_frame='date',
center=dict({'lat': 32, 'lon': 4}), zoom=0.7, hover_data= ['country'])
#here on wards various layouts have been called in to bring it in the present shape
fig_map.update_layout(mapbox_style="carto-positron",width=900,
height=700)
fig_map.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
#update frame speed
fig_map.layout.updatemenus[0].buttons[0].args[1]["frame"]["duration"] = 200
#update different layouts
fig_map.layout.sliders[0].currentvalue.xanchor="left"
fig_map.layout.sliders[0].currentvalue.offset=-100
fig_map.layout.sliders[0].currentvalue.prefix=""
fig_map.layout.sliders[0].len=.9
fig_map.layout.sliders[0].currentvalue.font.color="indianred"
fig_map.layout.sliders[0].currentvalue.font.size=20
fig_map.layout.sliders[0].y= 1.1
fig_map.layout.sliders[0].x= 0.15
fig_map.layout.updatemenus[0].y=1.27
fig_map.show()